<?php
class Data_File extends SplFileInfo
{

	public function __toString() {
		return $this->getFileName();
	}
	
	/*
	 * Renvoie le chemin complet vers le fichier
	 */
	public function getFilePath() {
		return $this->getPathname();
	}
	
	/**
	 * supprime l'élément
	 * 
	 */
	public function delete() {
		if (!$result = unlink($this->getFilePath())) {
			throw new App_Exception('Unable to delete file '.$this->getFilePath());
		}
	}
	
	/*
	 * Renvoie le chemin du répertoire parent
	 * @return string
	 */
	public function getParentPath() {
		return $this->getPath();
	}
	
	/*
	 * Renvoie le répertoire parent
	 * @return Data_Directory
	 */
	public function getParent() {
		return new Data_Directory($this->getParentPath());
	}
	
	/**
	 * Déplace le fichier vers le répertoire séléctionné
	 *
	 * @param Data_Directory $dir
	 */
	public function moveTo(Data_Directory $dir) {
		$target = $dir->getFilePath().DIRECTORY_SEPARATOR.$this->getFileName();
		if (!$result = rename($this->getFilePath(),$target)) {
			throw new App_Exception('Unable to move file '.$this->getFilePath().' to '.$dir->getFilePath());
		}
	}

	/**
	 * renome le fichier
	 *
	 * @param Data_Directory $dir
	 */
	public function rename($newName) {
		$target = $this->getParentPath().DIRECTORY_SEPARATOR.$newName;
		if (!$result = rename($this->getFilePath(),$target)) {
			throw new App_Exception('Unable to rename file '.$this->getFilePath().' to '.$dir->getFilePath());
		}
	}
	
	/**
	 * Renvoie le chemin relatif à partir du Data_Directory passé.
	 * Le répertoire passé en paramètre doit être un parent du répertoire actuel.
	 * @param Data_Directory $dir
	 * @return string
	 */
	public function getRelativePathFrom(Data_Directory $dir)
	{
		return str_replace($dir->getFilePath(),'',$this->getFilePath());	
	}
	
	/**
	 * Envoie le fichier sur la sortie standart
	 */
	public function send() 
	{
		header("Content-Type: ".$this->getMimeType());
		header("Content-Length: " . $this->getSize());			
		$file = $this->openFile();
		$file->fpassthru();
	}
	
	public function getExtension()
	{
		preg_match('/^.*\.([^.]+?)?$/',$this->getFilepath(),$matches);
		return strtolower($matches[1]);
	}
	
	public function getMimeType()
	{
		switch ($this->getExtension()) {
			case 'jpg':
			case 'png':
			case 'gif':
				return 'image/'.$this->getExtension();
				break;
			default : 
				return 'test/plain';
		}
	}
	
	public function sendThumb($width=100, $height=100)
	{
		$cache = App_Cache::getInstance();
		$id = md5($this->getFilename());
		if (!($cache->test($id))) {
			switch ($this->getExtension()) {
				case 'jpg' :
				case 'jpeg': $im  = imagecreatefromjpeg ($this->getFilePath());
				break;
			case 'gif' : $im  = imagecreatefromgif  ($this->getFilePath());
				break;
			case 'png' : $im  = imagecreatefrompng  ($this->getFilePath());
				break;
			default :
				throw new App_Exception($this->getFilePath().' is not an image');	
			}
			
			header("Content-Type: image/jpg");
			
			$x = imagesx($im);
			$y = imagesy($im);
			
			if (($width/$width) < ($x/$y)) {
				$thumb = imagecreatetruecolor($x/($x/$width), $y/($x/$width));
			}
			else {
				$thumb = imagecreatetruecolor($x/($y/$height), $y/($y/$height));
			}
			imagecopyresized($thumb , $im, 0, 0, 0, 0, imagesx($thumb), imagesy($thumb), $x, $y);
			ob_start();
			imagejpeg($thumb,null,100);
			$serializedImage = ob_get_contents();
			ob_end_clean();
			$cache->save($serializedImage);
			imagejpeg($thumb,null,100);
		}
		else {
			header("Content-Type: image/jpg");
			$thumb = imagecreatefromstring($cache->get($id));
			imagejpeg($thumb,null,100);
		}
		
	}
}
?>